package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    //展示全部
    @RequestMapping("/selectAll")
    @LogAnnotation(value = "展示所有上师",type = "selectAll")
    public Map selectAll(int page,int limit,String guruName){


        return guruService.selectAll(page, limit, guruName);
    }

    //增加一条数据
    @RequestMapping("/insert")
    @LogAnnotation(value = "添加上师",type = "insert")
    public Map insert(Guru guru, MultipartFile file, HttpServletRequest request) throws IOException {

        //绝对路径
        String realPath = request.getServletContext().getRealPath("/img/guru");//绝对路径
        //做重命名防止文件名字被覆盖
        String newFilename=new Date().getTime()+"_"+file.getOriginalFilename();

        file.transferTo(new File(realPath+"/"+newFilename));

        guru.setGuruImage("/img/guru/"+newFilename);




        return guruService.insert(guru);
    }


    //删除一条数据
    @RequestMapping("/delete")
    @LogAnnotation(value = "删除上师",type = "delete")
    public Map delete(int id){
        return guruService.delete(id);
    }

    //修改一条数据
    @RequestMapping("/update")
    @LogAnnotation(value = "更新上师",type = "update")
    public Map update(Guru guru,HttpServletRequest request,MultipartFile file) throws IOException {
        //绝对路径
       if(file!=null){
           String realPath = request.getServletContext().getRealPath("/img/guru");//绝对路径
           //做重命名防止文件名字被覆盖
           String newFilename=new Date().getTime()+"_"+file.getOriginalFilename();

           file.transferTo(new File(realPath+"/"+newFilename));

           guru.setGuruImage("/img/guru/"+newFilename);

           return guruService.update(guru);
       }else{
           return guruService.updatea(guru);
       }
    }


    //查询一条数据
    @RequestMapping("/selectOne")
    @LogAnnotation(value = "展示单个上师",type = "selectOne")
    public Guru selectOne(int id){
        return guruService.selectOne(id);
    }


    //修改上师状态
    @RequestMapping("/updates")
    @LogAnnotation(value = "上师隐藏or展示",type = "updates")
    public Map updates(Guru guru){
//        guru.setGuruStatus(1);



        System.out.println(guru.getGuruId()+"guruIdController");
        System.out.println(guru.getGuruStatus()+"guruStatusController");


        return guruService.updates(guru);
    }


    //批量下载
    @RequestMapping("/download")
    @LogAnnotation(value = "导出上师数据",type = "download")
    public Map selectAlls(){
        return guruService.selectAlls();
    }


}
